import fs from 'node:fs'
import path from 'node:path'
import { log } from 'node:console'

/**
 * 深度优先后序遍历文件夹
 * @param {string} root 目录
 * @param {number} maxDepth 最大文件夹遍历层数
 * @returns {string[]} 返回绝对路径数组
 */
function walkDFS(root, maxDepth = null) {
    const files = []
    if (maxDepth !== null) {
        maxDepth -= 1
        if (maxDepth < 0)
            return files
    }
    let dirEnds = fs.readdirSync(root, { withFileTypes: true })
    for (let dirEnd of dirEnds) {
        let p = path.join(dirEnd.parentPath, dirEnd.name)
        if (dirEnd.isDirectory()) {
            files.push(...walkDFS(p, maxDepth))
        }
        files.push(path.resolve(p))
    }
    return files
}


let files = walkDFS('I:\\前端')
log(files)